//
//  ChooseCarVIew.m
//  CarHome
//
//  Created by 王建男 on 14-9-27.
//  Copyright (c) 2014年 WJN_work@163.com. All rights reserved.
//

#import "ChooseCarVIew.h"
#import "CarTableViewCell.h"
#import "UIImageView+WebCache.h"

#define DCENTER 160

NSString *const TableViewCellIdentifier_choose = @"Cell_FindCar_Choose";

@implementation ChooseCarVIew

- (void)dealloc
{
    [_tableView release];
    [_tableArray release];
    [_tureView release];
    [_titleArray release];
    _tableArray = nil;
    _tableView = nil;
    _tureView = nil;
    _titleArray = nil;
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        _tableArray = [[NSMutableArray alloc] init];
        
        [self connect];
        [self creatView];
        //添加手势
        UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
                                                        initWithTarget:self
                                                        action:@selector(handlePan:)];
        [self addGestureRecognizer:panGestureRecognizer];
        [panGestureRecognizer release];
    }
    return self;
}

#pragma mark -
#pragma mark 滑动手势

-(void) handlePan:(UIPanGestureRecognizer*) recognizer
{
    CGPoint point = [recognizer translationInView:self];
    float x = self.center.x + point.x;
    _button.backgroundColor = [UIColor clearColor];
    if (x < DCENTER) {
        x = self.center.x;
    }
    
    self.center = CGPointMake(x, self.center.y);
    //设置动画
    if(recognizer.state == UIGestureRecognizerStateEnded)
    {
        [UIView animateWithDuration:1
                              delay:0.01
                            options:UIViewAnimationCurveEaseInOut
                         animations:^{
                             if (x > 250) {
                                 self.center = CGPointMake(400, self.center.y);
                             } else {
                                 self.center = CGPointMake(DCENTER, self.center.y);
                                 _button.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
                             }
                         } completion:^(BOOL finished) {
                             
                         }];
    }
    [recognizer setTranslation:CGPointZero inView:self];
}

#pragma mark -
#pragma mark creatView

- (void)creatView
{
    _tureView = [[UIView alloc] initWithFrame:CGRectMake(80, 0, self.frame.size.width - 80, self.frame.size.height)];
    [_tureView setBackgroundColor:[UIColor whiteColor]];
    [self addSubview:_tureView];
    
    _button = [UIButton buttonWithType:UIButtonTypeCustom];
    [_button setFrame:CGRectMake(0, 0, self.frame.size.width - _tureView.frame.size.width, self.frame.size.height)];
    [_button setBackgroundColor:[UIColor colorWithWhite:0.3 alpha:0.7]];
    [_button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_button];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _tureView.frame.size.width, 64)];
    [lable setTextAlignment:NSTextAlignmentCenter];
    [lable setText:@"选择品牌"];
    [_tureView addSubview:lable];
    [lable release];
    
    [self creatTableView];
}

- (void)clickAction:(id)sender
{
    [self removeFromSuperview];
}

#pragma mark -
#pragma mark creatTableVIew

- (void)creatTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, _tureView.frame.size.width, _tureView.frame.size.height - 64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [_tableView registerClass:[CarTableViewCell class] forCellReuseIdentifier:TableViewCellIdentifier_choose];
    [_tureView addSubview:_tableView];
}

#pragma mark -
#pragma mark tableView协议

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[_tableArray objectAtIndex:section] count];
}

- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_tableArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CarTableViewCell * cellCar = [tableView dequeueReusableCellWithIdentifier:TableViewCellIdentifier_choose];
    NSString * arr = [[[_tableArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row] objectForKey:@"brandname"];
    NSString * picture = [[[_tableArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row] objectForKey:@"brandimg"];
    
    [cellCar.label setText:arr];
    [cellCar.myView sd_setImageWithURL:[NSURL URLWithString:picture] placeholderImage:[UIImage imageNamed:@"Loading.png"]];
    
    [cellCar.label setTextColor:[UIColor blackColor]];
    return cellCar;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSMutableString *string = [NSMutableString stringWithFormat:@"%@", [[[_tableArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row] objectForKey:@"brandimg"]];

    NSRange range = [string rangeOfString:@"http://x.autoimg.cn/app/image/brands/"];//删除字符串
    [string deleteCharactersInRange:range];

    NSRange rangeother = [string rangeOfString:@".png"];//删除字符串
    [string deleteCharactersInRange:rangeother];
    
    self.block(string);
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [_titleArray objectAtIndex:section];
}


#pragma mark -
#pragma mark 添加A-Z字母
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
//    NSMutableArray * array = [[NSMutableArray alloc]init];
//        for (int i = 0; i<26; i++) {
//            NSString * name = [NSString stringWithFormat:@"%c",65+i];
//            [array addObject:name];
//        }
    return _titleArray;
}

#pragma mark -
#pragma mark 网络连接

- (void)connect
{
   [NetUrl connect:carcar parmater:nil Block:^(NSData *data) {
    
      NSMutableDictionary * bigDic = (NSMutableDictionary *)data;
      id object =  [bigDic objectForKey:@"result"];
      NSMutableDictionary * dic1 = [NSMutableDictionary dictionaryWithDictionary:object];
       
       NSMutableArray *_bigArray = [[NSMutableArray alloc] init];
       _titleArray = [[NSMutableArray alloc] init];
       
       _bigArray = [NSMutableArray arrayWithArray:[dic1 objectForKey:@"list"]];
       
   //数组添加车型
       
    for (NSDictionary *dic in _bigArray) {
        [_titleArray addObject:[dic objectForKey:@"letter"]];
    }
       
    NSSet *set = [NSSet setWithArray:_titleArray];
    _titleArray = [(NSMutableArray *)[set allObjects]retain];
       
    for (int i = 0; i < [_titleArray count]; i++) {
        NSMutableArray *array = [[NSMutableArray alloc] init];
        [_tableArray addObject:array];
        [array release];
    }
    
    
    
    for (NSDictionary *dic in _bigArray) {
        [[_tableArray objectAtIndex:[_titleArray indexOfObject:[dic objectForKey:@"letter"]]] addObject:dic];
    }
       
       [_tableView reloadData];
}];
}

- (void)showBlock:(ChooseBlock )block
{
    self.block = block;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
